package control;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import entities.User;
import GUI.MainFrame;

/**
 * GUI controllers for the Librarian
 * @author Lidor
 *
 */
	public class LibrarianControllers{
		
		public MainFrame mf;
		public User user;
/**
 * Create the panel.
 * @param M main frame of the program
 */
		public LibrarianControllers(MainFrame M){
		mf=M;
		}
/**
 * Display the appropriate data into the GUI depending on command		
 * @param msg
 */
		public void handleMessage (Object msg){
			ArrayList<Object> messages=(ArrayList<Object>) msg;
			String command = (String) messages.get(1);
			
			if(messages.get(1).equals("ViewUserCard2"))
			{
				if(messages.get(2).equals("OK"))
				{
					user=(User)messages.get(3);
					Integer id=user.id;
					mf.librarianMain.viewUser.textName.setText(user.username);
					mf.librarianMain.viewUser.textID.setText(id.toString());
					mf.librarianMain.viewUser.textEmail.setText(user.email);
					mf.librarianMain.viewUser.textPhone.setText(user.phone);
					if(user.status.equals("1"))
					mf.librarianMain.viewUser.textUserStatus.setText("Logged In");
					else mf.librarianMain.viewUser.textUserStatus.setText("Not Logged In");
						
				}
				if(messages.get(2).equals("Error")) JOptionPane.showMessageDialog(null, "ERROR!", "Program", JOptionPane.PLAIN_MESSAGE);

			}
			
			if(messages.get(1).equals("AddNewUser"))
			{
				if(messages.get(2).equals("OK"))
				{
					mf.librarianMain.registerNewUser.init();
					mf.librarianMain.registerNewUser.textUserID.setText(messages.get(3).toString());
					mf.librarianMain.registerNewUser.textPassword.setText(messages.get(4).toString());
					JOptionPane.showMessageDialog(null, "User was added succesfully!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
			}
			
			if(messages.get(1).equals("ManualExtension"))
			{
				if(messages.get(2).equals("OK"))
				{
					JOptionPane.showMessageDialog(null, "Extended Successfuly!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
				else JOptionPane.showMessageDialog(null, "Extension Failed!", "Program", JOptionPane.ERROR_MESSAGE);
			}
			
			if(messages.get(1).equals("BorrowBook"))
			{
				if(messages.get(2).equals("OK"))
				{
					mf.librarianMain.bookBorrow.init();
					JOptionPane.showMessageDialog(null, "Book was borrowed succesfully!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
				if(messages.get(2).equals("Fail")) 
				{
					mf.librarianMain.bookBorrow.init();
					JOptionPane.showMessageDialog(null, "Borrow book failed!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
			}
			if(messages.get(1).equals("ReturnBook"))
			{
				if(messages.get(2).equals("OK"))
				{
					mf.librarianMain.bookReturn.init();
					JOptionPane.showMessageDialog(null, "Book was returned succesfully!", "Program", JOptionPane.PLAIN_MESSAGE);	
				}
				else
				{
					mf.librarianMain.bookReturn.init();
					JOptionPane.showMessageDialog(null, "Borrow return failed!", "Program", JOptionPane.PLAIN_MESSAGE);	
				}	
			}
			
			if(messages.get(1).toString().equals("ViewBorrowsForUser"))
			{
				if(messages.get(2).equals("OK"))
				{
					String[] borrow;

	                for(int i=3;i<messages.size();i++)
	                {
	                	borrow = (String[]) messages.get(i);
	                    String[] borrowInfo = {borrow[0],borrow[1],borrow[2],borrow[3]};
						System.out.println(borrow[0]);
						System.out.println(borrow[1]);
	                    mf.librarianMain.viewUser.searchTableModel1.addRow(borrowInfo);
	                }
				}
				else
				{
					mf.librarianMain.viewUser.init();
				}
			}
			
			if(messages.get(1).toString().equals("addBook"))
			{
				if(messages.get(2).equals("OK"))
				{
					JOptionPane.showMessageDialog(null, "book added successfuly", "Program", JOptionPane.PLAIN_MESSAGE);
				}
				else
				{
					JOptionPane.showMessageDialog(null, "book add failed", "Program", JOptionPane.PLAIN_MESSAGE);
				}
			}
		}
	}